/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Xml.XPath;
using DaveSexton.DocProject.Sandcastle.BuildComponents.TypeConverters.BuiltIn;
using System.Xml;

namespace DaveSexton.DocProject.Sandcastle.BuildComponents.Editors.BuiltIn
{
	internal sealed partial class ResolveConceptualLinksComponentEditorDialog : DaveSexton.DocProject.Sandcastle.BuildComponents.Editors.CancelableBaseEditorDialog
	{
		#region Public Properties
		#endregion

		#region Private / Protected
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ResolveConceptualLinksComponentEditorDialog" /> class.
		/// </summary>
		public ResolveConceptualLinksComponentEditorDialog()
		{
			InitializeComponent();

			foreach (int linkType in Enum.GetValues(typeof(ReferenceLinkType)))
				linkTypeColumn.Items.Add((ReferenceLinkType) linkType);
		}
		#endregion

		#region Methods
		protected override void UpdateForBuildComponent(SandcastleBuildComponent component)
		{
			XPathNavigator navigator = component.CreateNavigator();

			SuspendLayout();

			try
			{
				bool showBrokenLinkText;
				if (bool.TryParse(navigator.GetAttribute("showBrokenLinkTextCheckBox", ""), out showBrokenLinkText))
					showBrokenLinkTextCheckBox.Checked = showBrokenLinkText;

				targetsBindingSource.DataSource = CreateTargetsDataTable(navigator);
			}
			finally
			{
				ResumeLayout();
			}
		}

		private DataTable CreateTargetsDataTable(IXPathNavigable component)
		{
			DataTable table = new DataTable();
			table.Locale = System.Globalization.CultureInfo.CurrentCulture;

			DataColumn column = table.Columns.Add("base", typeof(string));
			column.AllowDBNull = false;
			column.DefaultValue = string.Empty;

			column = table.Columns.Add("files", typeof(string));
			column.AllowDBNull = false;
			column.DefaultValue = string.Empty;

			column = table.Columns.Add("url", typeof(string));
			column.AllowDBNull = false;
			column.DefaultValue = string.Empty;

			column = table.Columns.Add("text", typeof(string));
			column.AllowDBNull = false;
			column.DefaultValue = string.Empty;

			column = table.Columns.Add("type", typeof(ReferenceLinkType));
			column.AllowDBNull = false;

			HelpTypes helpTypes = BuildComponent.ConfigurationHelpTypes;

			column.DefaultValue = ((helpTypes & HelpTypes.Help1x) != 0)
				? ReferenceLinkType.Local
				: ((helpTypes & HelpTypes.Help2x) != 0)
					? ReferenceLinkType.Index
					: ReferenceLinkType.None;

			ResolveReferenceLinksComponent2EditorDialog.FillTargetsDataTable(component, table);

			return table;
		}

		private void SaveAttributes()
		{
			XPathNavigator navigator = BuildComponent.CreateNavigator();

			bool showBrokenLinkText = showBrokenLinkTextCheckBox.Checked;

			XPathNavigator clone = navigator.Clone();

			if (clone.MoveToAttribute("showBrokenLinkText", ""))
			{
				if (showBrokenLinkText)
					clone.SetValue("true");
				else
					clone.DeleteSelf();
			}
			else if (showBrokenLinkText)
				clone.CreateAttribute(null, "showBrokenLinkText", null, "true");
		}

		protected override bool SaveChanges()
		{
			if (!ValidateChildren())
				return false;

			DataTable table = targetsBindingSource.DataSource as DataTable;

			if (table != null)
			{
				SaveAttributes();
				ResolveReferenceLinksComponent2EditorDialog.SaveTargetsDataTable(BuildComponent, table);
			}

			return true;
		}
		#endregion

		#region Event Handlers
		private void targetsDataGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
		{
			e.Cancel = true;
		}
		#endregion
	}
}